package hms.RoomManagementModule;

import hms.permissions.Permission;
import hms.properties.Properties;
import hms.rooms.Room;
import hms.rooms.RoomTable;

import java.awt.Font;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;

public class ManageRoomsPanel extends JPanel {
	protected JButton btn_decommission;
	protected JButton btn_reservationStatus;
	protected JButton btn_more;
	
	protected JLabel lbl_roomType, lbl_roomLabel, lbl_roomStatus;
	
	protected JList list_roomList;
	protected DefaultListModel listModel;
	
	/**
	 * Enable the decommission button
	 */
	public void enableDecommissionButton(){
		if(Properties.currentUser.isPermissionGranted(Permission.CONFIGURE_ROOMS))
			btn_decommission.setEnabled(true);
	}
	
	/**
	 * Sets the text of the Decommission button.
	 */
	public void setDecommissionButtonText(Room selectedRoom){
		if(selectedRoom.getOccupationStatus().equalsIgnoreCase("out of commission"))
			btn_decommission.setText("Re-commission");
		else
			btn_decommission.setText("Decommission...");
	}
	
	/**
	 * Enable the reservation status button
	 */
	public void enableReservationStatusButton(){
		btn_reservationStatus.setEnabled(true);
	}
	
	/**
	 * Enable the More... button
	 */
	public void enableMoreButton(){
		btn_more.setEnabled(true);
	}
	
	/**
	 * Fill out the components with information from the database
	 */
	public void fillOutComponents(){
		RoomTable rooms = new RoomTable();
		for(int i = 0; i < rooms.getNumberOfRooms(); i++){
			listModel.addElement(rooms.getElementAt(i).toString());
		}
	}
	
	/**
	 * Create the panel. This code was automatically created using WindowsBuilder for Eclipse. I
	 * added in some extra things and moved some variables to become global, but for the most part
	 * everything in here was actually performed by hand so the code is not hand written.
	 */
	public ManageRoomsPanel(ManageRoomsView view) {
		
		listModel = new DefaultListModel();
		
		JPanel panel = new JPanel();
		
		JPanel panel_roomStatus = new JPanel();
		panel_roomStatus.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		JLabel lbl_roomStatusHeader = new JLabel("Room Status:");
		lbl_roomStatusHeader.setFont(new Font("SansSerif", Font.PLAIN, 20));
		
		JLabel lbl_roomsList = new JLabel("Rooms:");
		lbl_roomsList.setFont(new Font("SansSerif", Font.PLAIN, 20));
		
		list_roomList = new JList(listModel);
		list_roomList.addListSelectionListener(view);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(list_roomList);

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(0)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(panel_roomStatus, GroupLayout.DEFAULT_SIZE, 920, Short.MAX_VALUE)
								.addComponent(lbl_roomStatusHeader))))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lbl_roomsList, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
							.addGap(259))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 358, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(lbl_roomsList, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 643, Short.MAX_VALUE)
					.addGap(18))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(257)
					.addComponent(lbl_roomStatusHeader)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_roomStatus, GroupLayout.PREFERRED_SIZE, 239, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(193, Short.MAX_VALUE))
		);
		
		btn_decommission = new JButton("Decommission...");
		btn_decommission.setActionCommand("btn_decommission");
		btn_decommission.setEnabled(false);
		btn_decommission.addActionListener(view);
		btn_decommission.setFont(new Font("SansSerif", Font.PLAIN, 16));
		
		
		
		btn_reservationStatus = new JButton("Reservation Status");
		btn_reservationStatus.setActionCommand("btn_reservationStatus");
		btn_reservationStatus.setEnabled(false);
		btn_reservationStatus.addActionListener(view);
		btn_reservationStatus.setFont(new Font("SansSerif", Font.PLAIN, 16));
		
		btn_more = new JButton("More...");
		btn_more.setActionCommand("btn_more");
		btn_more.setEnabled(false);
		btn_more.addActionListener(view);
		btn_more.setFont(new Font("SansSerif", Font.PLAIN, 16));
		
		if(!Properties.currentUser.isPermissionGranted(Permission.CONFIGURE_ROOMS)) {
			btn_decommission.setVisible(false);
		}
		
		lbl_roomLabel = new JLabel("Room: ");
		lbl_roomLabel.setFont(new Font("SansSerif", Font.PLAIN, 25));
		
		lbl_roomStatus = new JLabel("Status:");
		lbl_roomStatus.setFont(new Font("SansSerif", Font.PLAIN, 25));
		
		lbl_roomType = new JLabel("Type:");
		lbl_roomType.setFont(new Font("SansSerif", Font.PLAIN, 25));
		GroupLayout gl_panel_roomStatus = new GroupLayout(panel_roomStatus);
		gl_panel_roomStatus.setHorizontalGroup(
			gl_panel_roomStatus.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_roomStatus.createSequentialGroup()
					.addGroup(gl_panel_roomStatus.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_roomStatus.createSequentialGroup()
							.addContainerGap()
							.addComponent(btn_decommission, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE)
							.addGap(77)
							.addComponent(btn_reservationStatus, GroupLayout.PREFERRED_SIZE, 308, Short.MAX_VALUE)
							.addGap(69))
						.addGroup(gl_panel_roomStatus.createSequentialGroup()
							.addGap(22)
							.addGroup(gl_panel_roomStatus.createParallelGroup(Alignment.LEADING)
								.addComponent(lbl_roomStatus, GroupLayout.PREFERRED_SIZE, 456, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_panel_roomStatus.createSequentialGroup()
									.addComponent(lbl_roomLabel)
									.addGap(257)
									.addComponent(lbl_roomType, GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)))
							.addGap(13)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btn_more, GroupLayout.PREFERRED_SIZE, 169, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		gl_panel_roomStatus.setVerticalGroup(
			gl_panel_roomStatus.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_roomStatus.createSequentialGroup()
					.addGap(25)
					.addGroup(gl_panel_roomStatus.createParallelGroup(Alignment.BASELINE)
						.addComponent(lbl_roomLabel)
						.addComponent(lbl_roomType, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
					.addGap(43)
					.addComponent(lbl_roomStatus, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
					.addGroup(gl_panel_roomStatus.createParallelGroup(Alignment.LEADING)
						.addComponent(btn_more, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_roomStatus.createParallelGroup(Alignment.BASELINE)
							.addComponent(btn_decommission, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
							.addComponent(btn_reservationStatus, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		panel_roomStatus.setLayout(gl_panel_roomStatus);
		setLayout(groupLayout);
		
		fillOutComponents();

	}
}
